/* tree_test-window.c
 *
 * Copyright 2024 wan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "tree_test-config.h"
#include "tree_test-window.h"

struct _TreeTestWindow
{
  GtkApplicationWindow  parent_instance;

  /* Template widgets */
  GtkHeaderBar        *header_bar;
  GtkTreeView           *label;
  GtkListStore *store; //ui中已经设计好
  GtkMenu *my_menu;
};

G_DEFINE_TYPE (TreeTestWindow, tree_test_window, GTK_TYPE_APPLICATION_WINDOW)

static void
tree_test_window_class_init (TreeTestWindowClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/a/b/c/tree_test-window.ui");
  gtk_widget_class_bind_template_child (widget_class, TreeTestWindow, header_bar);
  gtk_widget_class_bind_template_child (widget_class, TreeTestWindow, label);
  gtk_widget_class_bind_template_child (widget_class, TreeTestWindow, store);//绑定到窗体
  gtk_widget_class_bind_template_child (widget_class, TreeTestWindow, my_menu);//绑定到窗体
}

gboolean open_context (GtkWidget *treeview, GdkEventButton *event, gpointer data) {
    if (event->button == 3) {  // 右键单击
        GtkWidget *menu = GTK_WIDGET (data);  // 创建你的自定义菜单
        gtk_menu_popup_at_pointer(GTK_MENU(menu), (GdkEvent *)event);
    }
   return FALSE;                                                                                 
}

static void
tree_test_window_init (TreeTestWindow *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));
   //绑定model
  gtk_tree_view_set_model(GTK_TREE_VIEW (self->label),GTK_TREE_MODEL(self->store));
  GtkCellRenderer *renderer;
  renderer = gtk_cell_renderer_text_new();
  // 创建一个列，将渲染器添加到列中
  GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes("id", renderer, "text", 0, NULL);
  
  gtk_tree_view_append_column(GTK_TREE_VIEW(self->label), column);

  column = gtk_tree_view_column_new_with_attributes("name", renderer, "text", 1, NULL);
  gtk_tree_view_append_column(GTK_TREE_VIEW(self->label), column);
  
  g_signal_connect(GTK_TREE_VIEW(self->label), "button-press-event", G_CALLBACK(open_context), self->my_menu);
  
  
  
}
